<?php

/**
 * Zend_Controller_Request_Http
 *
 * @author Zend Framework
 * @modifier JianJian <silverd@sohu.com>
 * $Id: Http.php 150 2012-11-06 02:48:52Z Jun.Lu.726@gmail.com $
 */

class Core_Request_Http extends Core_Request_Abstract
{
    public function __construct()
    {
        // 保证GPC都是没有经过 addslashes 的
        if (get_magic_quotes_gpc()) {
            $_GET     = $this->stripslashes($_GET);
            $_POST    = $this->stripslashes($_POST);
            $_COOKIE  = $this->stripslashes($_COOKIE);
            $_REQUEST = $this->stripslashes($_REQUEST);
        }
    }

    public function stripslashes($data)
    {
        return is_array($data) ? array_map(array($this, 'stripslashes'), $data) : stripslashes($data);
    }

    public function __get($key)
    {
        switch (true) {
            case isset($this->_params[$key]):
                return $this->_params[$key];
            case isset($_GET[$key]):
                return $_GET[$key];
            case isset($_POST[$key]):
                return $_POST[$key];
            case isset($_COOKIE[$key]):
                return $_COOKIE[$key];
            case isset($_SERVER[$key]):
                return $_SERVER[$key];
            case isset($_ENV[$key]):
                return $_ENV[$key];
            default:
                return null;
        }
    }

    public function __isset($key)
    {
        switch (true) {
            case isset($this->_params[$key]):
                return true;
            case isset($_GET[$key]):
                return true;
            case isset($_POST[$key]):
                return true;
            case isset($_COOKIE[$key]):
                return true;
            case isset($_SERVER[$key]):
                return true;
            case isset($_ENV[$key]):
                return true;
            default:
                return false;
        }
    }

    public function get($key)
    {
        return $this->__get($key);
    }

    public function has($key)
    {
        return $this->__isset($key);
    }

    public function getQuery($key = null, $default = null)
    {
        if (null === $key) {
            return $_GET;
        }

        return (isset($_GET[$key])) ? $_GET[$key] : $default;
    }

    public function getPost($key = null, $default = null)
    {
        if (null === $key) {
            return $_POST;
        }

        return (isset($_POST[$key])) ? $_POST[$key] : $default;
    }

    public function getCookie($key = null, $default = null)
    {
        if (null === $key) {
            return $_COOKIE;
        }

        return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
    }

    public function getServer($key = null, $default = null)
    {
        if (null === $key) {
            return $_SERVER;
        }

        return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
    }

    public function getEnv($key = null, $default = null)
    {
        if (null === $key) {
            return $_ENV;
        }

        return (isset($_ENV[$key])) ? $_ENV[$key] : $default;
    }

    public function getParam($key = null, $default = null)
    {
        if (null === $key) {
            return $this->getParams();
        }

        if (isset($this->_params[$key])) {
            return $this->_params[$key];
        } elseif (isset($_GET[$key])) {
            return $_GET[$key];
        } elseif (isset($_POST[$key])) {
            return $_POST[$key];
        }

        return $default;
    }

    public function getParams()
    {
        $return = $this->_params;

        if (isset($_GET) && is_array($_GET)) {
            $return += $_GET;
        }
        if (isset($_POST) && is_array($_POST)) {
            $return += $_POST;
        }

        return $return;
    }

    public function setParam($key, $value)
    {
        $key = (string) $key;

        if ((null === $value) && isset($this->_params[$key])) {
            unset($this->_params[$key]);
        } elseif (null !== $value) {
            $this->_params[$key] = $value;
        }

        return $this;
    }

    public function setParams(array $params)
    {
        foreach ($params as $key => $value) {
            $this->setParam($key, $value);
        }

        return $this;
    }

    public function isPost()
    {
        return ('POST' == $_SERVER['REQUEST_METHOD']);
    }

    public function isGet()
    {
        return ('GET' == $_SERVER['REQUEST_METHOD']);
    }

    public function isPut()
    {
        return ('PUT' == $_SERVER['REQUEST_METHOD']);
    }

    public function isDelete()
    {
        return ('DELETE' == $_SERVER['REQUEST_METHOD']);
    }

    public function isHead()
    {
        return ('HEAD' == $_SERVER['REQUEST_METHOD']);
    }

    public function isOptions()
    {
        return ('OPTIONS' == $_SERVER['REQUEST_METHOD']);
    }

    public function getRawBody()
    {
        $body = file_get_contents('php://input');

        if (strlen(trim($body)) > 0) {
            return $body;
        }

        return false;
    }

    public function getHeader($header)
    {
        if (empty($header)) {
            return false;
        }

        $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
        if (!empty($_SERVER[$temp])) {
            return $_SERVER[$temp];
        }

        // Apache
        if (function_exists('apache_request_headers')) {
            $headers = apache_request_headers();
            if (!empty($headers[$header])) {
                return $headers[$header];
            }
        }

        return false;
    }

    public function isXmlHttpRequest()
    {
        return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
    }

    public function isFlashRequest()
    {
        return ($this->getHeader('USER_AGENT') == 'Shockwave Flash');
    }

    public function isCli()
    {
        return (bool) defined('STDIN');
    }

    public function getUserAgent()
    {
        return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }

    public function getHttpReferer()
    {
        return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    }
}